import java.util.ArrayList;
import java.util.List;

public class YgjRabbit {
    private List<Rabbit> rabbits;
    private int month = 0;

    public static void main(final String[] args) {
        final YgjRabbit ygjRabbit = new YgjRabbit();
        ygjRabbit.rabbits = new ArrayList<>();
        ygjRabbit.rabbits.add(new Rabbit(0, 0));
        ygjRabbit.rabbits.add(new Rabbit(0, 1));

        ygjRabbit.calculate();
        System.out.println("方法1兔子对数：" + ygjRabbit.rabbits.size() / 2);
        System.out.println("方法2兔子对数：" + ygjRabbit.calculate(12));

    }

    /**
     * 方法2
     * 根据方法1推算，除第一、二个月，后面的月份兔子对数是前两个月兔子对数之和
     * 
     * @param endMonth
     * @return
     */
    private int calculate(int endMonth) {
        if (endMonth == 1 || endMonth == 2) {
            return 1;
        } else {
            return calculate(endMonth - 1) + calculate(endMonth - 2);
        }
    }

    /**
     * 方法1
     * 假设兔子出生都在月初，在每月末统计，那么新增兔子应该是一个月大
     */
    private void calculate() {
        if (++month <= 12) {
            List<Rabbit> tempRabbits = new ArrayList<>();
            for (int i = 0; i < rabbits.size(); i++) {
                Rabbit rabbit = rabbits.get(i);

                if (rabbit.getAge() >= 2 && rabbit.sex == 0) {
                    Rabbit femaleRabbit = new Rabbit(1, 0);
                    Rabbit maleRabbit = new Rabbit(1, 1);
                    tempRabbits.add(femaleRabbit);
                    tempRabbits.add(maleRabbit);
                }
                rabbit.age = rabbit.age + 1;
            }
            rabbits.addAll(tempRabbits);
            System.out.println("月份：" + month + ",兔子对数：" + rabbits.size() / 2);
            calculate();
        }
    }

    public static class Rabbit {
        int age;
        /**
         * 0:female,1:male
         */
        int sex;

        Rabbit(final int age, final int sex) {
            this.age = age;
            this.sex = sex;
        }

        public void setAge(final int age) {
            this.age = age;
        }

        public int getAge() {
            return age;
        }

        public void setSex(final int sex) {
            this.sex = sex;
        }

        public int getSex() {
            return sex;
        }
    }
}